import { defineTool, exec } from "@/utils.js"

export async function recent(): Promise<string> {
  // 获取最近10次提交的首行
  const onelineCmd = "git log --oneline -10"
  const onelineOutput = (await exec(onelineCmd)).toString("utf8").trim()

  // 获取最近3次提交的完整信息
  const fullCmd = "git log -3 --stat --pretty=full"
  const fullOutput = (await exec(fullCmd)).toString("utf8").trim()

  // 组合输出
  let result = "最近10次提交(首行):\n"
  result += onelineOutput || "(无提交)"
  result += "\n\n最近3次提交(完整信息):\n"
  result += fullOutput || "(无提交)"
  return result
}

export default defineTool({
  name: "git_recent",
  description: "查看最近的 Git 提交信息以做参考",
  async callback(_extra) {
    return { content: [{ type: "text", text: (await recent()) || "No recent commits" }] }
  },
})
